package com.lcq.demo.controller;

import com.google.common.base.Preconditions;
import com.lcq.demo.common.entity.PageResult;
import com.lcq.demo.common.entity.Result;
import com.lcq.demo.entity.dto.ProductDTO;
import com.lcq.demo.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author 240701005
 */
@Tag(name = "产品管理")
@RestController
@RequestMapping("/api/products")
@Slf4j
//@CrossOrigin(origins = "*", allowedHeaders = "token")
public class ProductController {

    @Resource
    private ProductService productService;

    /**
     * 根据Id查询产品
     */
    @Operation(summary = "根据Id查询产品")
    @GetMapping("/{id}")
    public Result<ProductDTO> getById(@PathVariable Long id) {
        try {
            if (log.isInfoEnabled()) {
                log.info("ProductController.getById.id:{}", id);
            }
            Preconditions.checkNotNull(id, "产品Id不能为空！");
            return Result.ok(productService.getById(id));
        } catch (Exception e) {
            log.error("ProductController.getById.error:{}", e.getMessage(), e);
            return Result.fail("查询产品失败！");
        }
    }

    /**
     * 新增产品
     */
    @Operation(summary = "新增产品")
    @PostMapping
    public Result<Boolean> addProduct(@RequestBody ProductDTO productDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("ProductController.addProduct.productDTO:{}", productDTO);
            }
            Preconditions.checkArgument(!StringUtils.isEmpty(productDTO.getProductName()), "产品名不能为空！");
            Preconditions.checkArgument(!StringUtils.isEmpty(productDTO.getProductType()), "产品类型不能为空！");
            Preconditions.checkArgument(!StringUtils.isEmpty(productDTO.getImgUrl()), "产品图片url不能为空！");
            Preconditions.checkArgument(!StringUtils.isEmpty(productDTO.getProductDescription()), "产品描述不能为空！");
            Preconditions.checkNotNull(productDTO.getSortKey(), "产品sortKey不能为空！");
            return Result.ok(productService.insert(productDTO));
        } catch (Exception e) {
            log.error("ProductController.addProduct.error:{}", e.getMessage(), e);
            return Result.fail("新增产品失败！");

        }
    }

    /**
     * 更新产品
     */
    @Operation(summary = "更新产品")
    @PutMapping
    public Result<Boolean> updateProduct(@RequestBody ProductDTO productDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("ProductController.updateProduct.productDTO:{}", productDTO);
            }
            Preconditions.checkNotNull(productDTO.getId(), "产品Id不能为空！");
            return Result.ok(productService.update(productDTO));
        } catch (Exception e) {
            log.error("ProductController.updateProduct.error:{}", e.getMessage(), e);
            return Result.fail("更新产品失败！");
        }
    }

    /**
     * 根据ID删除产品
     */
    @Operation(summary = "删除产品")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteProduct(@PathVariable Long id) {
        try {
            if (log.isInfoEnabled()) {
                log.info("ProductController.deleteProduct.id:{}", id);
            }
            Preconditions.checkNotNull(id, "产品Id不能为空！");
            return Result.ok(productService.deleteById(id));
        } catch (Exception e) {
            log.error("ProductController.deleteProduct.error:{}", e.getMessage(), e);
            return Result.fail("删除产品失败！");
        }
    }

    /**
     * 获取产品列表
     */
    @Operation(summary = "获取产品列表", description = "条件查询")
    @PostMapping("/list")
    public Result<PageResult<ProductDTO>> getProduct(@RequestBody ProductDTO productDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("ProductController.getProduct.productDTO:{}", productDTO);
            }
            return Result.ok(productService.getProducts(productDTO));
        } catch (Exception e) {
            log.error("ProductController.getProduct.error:{}", e.getMessage(), e);
            return Result.fail("查询产品列表失败！");
        }
    }

}
